<?php

namespace App\Http\Controllers;

use App\Models\Campus;
use App\Models\Routes;
use App\Models\Staffs;
use Illuminate\Http\Request;

class RouteController extends Controller
{
    public function getRoutes(Request $request){
        $staff = Staffs::where("id" , $request->object_id)->first();
        $order = $staff->route_id == 1 ? 'asc' : 'desc';
        $routes = Campus::orderBy('id' , $order)->get();
        $json = [];
        $num = 1;
        foreach($routes as $route){
            $json[] = [
                'id' => $route->id,
                'order' => $num++,
                'name' => $route->name,
                'current' => $staff->campus_id == $route->id ? 1 : 0
            ];
        }
        return $this->returnJson($json);
    }

    public function getNextRoute(Request $request){
        $staff = Staffs::where("id" , $request->object_id)->first();
        $routes = Campus::all();
        if ($staff->route_id == 1) {
            $next = $staff->campus_id;
        }else{
            $next = $staff->campus_id - 2;
        }
        if ($next >= $routes->count()) {
            $next = $routes->count() - 1;
        }
        if ($next < 0) {
            $next = 0;
        }
        return $this->returnJson([
            'campus' => $routes[$next]->name,
        ]);
    }
}
